package com.sgame.smobigame;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class GamesView extends Activity {
	ListView lvMyGame, lvFriendGame;
	
	GameAdapter adp1, adp2;
	TextView tvGameFriend;
	ImageView ivAllGame;
	
	JSONArray jaMyGame = null;
	JSONArray jaFriGame = null;
	
	static String url = ""; // API
	static final String TAG_MyGame = "my_game";
	static final String TAG_NameMyGame = "name";
	static final String TAG_DescMyGame = "description";
	static final String TAG_ImageMyGame = "imgGame";
	static final String TAG_MyOptions = "imgOptions";
	static final String TAG_FriGame = "friend_game";
	static final String TAG_NameFriGame = "name";
	static final String TAG_DescFriGame = "description";
	static final String TAG_ImageFriGame = "imgGame";
	static final String TAG_FriOptions = "imgOptions";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sgame_game);

		tvGameFriend = (TextView) findViewById(R.id.tvFriendGame);
		ivAllGame = (ImageView) findViewById(R.id.imgALlGame);
	lvMyGame();
	lvFriendGame();
	}

	private void lvFriendGame() {// Hashmap for ListView
		ArrayList<HashMap<String, String>> listFriGame = new ArrayList<HashMap<String, String>>();

		// Creating JSON Parser instance
		JSONParser jParser = new JSONParser();

		// getting JSON string from URL
		JSONObject json = jParser.getJSONFromUrl(url);
		try {
			// Getting Array of Contacts
			jaFriGame = json.getJSONArray(TAG_FriGame);

			// looping through All Contacts
			for (int i = 0; i < jaMyGame.length(); i++) {
				JSONObject c = jaMyGame.getJSONObject(i);

				// Storing each json item in variable
				String stMyGame = c.getString(TAG_NameMyGame);
				String stDescMyGame = c.getString(TAG_DescMyGame);
				String stImageMyGame = c.getString(TAG_ImageMyGame);
				String stMyOptions = c.getString(TAG_MyOptions);

				// creating new HashMap
				HashMap<String, String> map = new HashMap<String, String>();

				// adding each child node to HashMap key => value
				map.put(TAG_NameMyGame, stMyGame);
				map.put(TAG_DescMyGame, stDescMyGame);
				map.put(TAG_ImageMyGame, stImageMyGame);
				map.put(TAG_MyOptions, stMyOptions);

				// adding HashList to ArrayList
				listFriGame.add(map);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		lvFriendGame = (ListView) findViewById(R.id.lvFriendGame);
		adp2 = new GameAdapter(this, listFriGame);
		lvFriendGame.setAdapter(adp2);

		// Click event for single list row
		lvFriendGame.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

			}
		});

	}

	private void lvMyGame() {
		// Hashmap for ListView
		ArrayList<HashMap<String, String>> listMyGame = new ArrayList<HashMap<String, String>>();

		// Creating JSON Parser instance
		JSONParser jParser = new JSONParser();

		// getting JSON string from URL
		JSONObject json = jParser.getJSONFromUrl(url);
		try {
			// Getting Array of Contacts
			jaMyGame = json.getJSONArray(TAG_MyGame);

			// looping through All Contacts
			for (int i = 0; i < jaMyGame.length(); i++) {
				JSONObject c = jaMyGame.getJSONObject(i);

				// Storing each json item in variable
				String stMyGame = c.getString(TAG_NameMyGame);
				String stDescMyGame = c.getString(TAG_DescMyGame);
				String stImageMyGame = c.getString(TAG_ImageMyGame);
				String stMyOptions = c.getString(TAG_MyOptions);

				// creating new HashMap
				HashMap<String, String> map = new HashMap<String, String>();

				// adding each child node to HashMap key => value
				map.put(TAG_NameMyGame, stMyGame);
				map.put(TAG_DescMyGame, stDescMyGame);
				map.put(TAG_ImageMyGame, stImageMyGame);
				map.put(TAG_MyOptions, stMyOptions);

				// adding HashList to ArrayList
				listMyGame.add(map);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		lvMyGame = (ListView) findViewById(R.id.lvMyGame);
		adp1 = new GameAdapter(this, listMyGame);
		lvMyGame.setAdapter(adp1);

		// Click event for single list row
		lvMyGame.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

			}
		});

	
	}

}
